package com.lbw.servlet;

import com.lbw.model.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lbw
 * @version 1.0
 * @date 2021/4/1 下午6:14
 */
@WebServlet(name = "StuServlet", urlPatterns = {"/StuServlet"})
public class StuServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        String stuId = request.getParameter("stu-id");
        String clazz = request.getParameter("clazz");
        String kind = request.getParameter("kind");
        String age = request.getParameter("age");
        String sex = request.getParameter("sex");
        String major = request.getParameter("major");
//        System.out.println(name + " " + stuId + " " + " " + clazz + " " +  kind);
        Student stu = new Student(name, stuId, age, sex, major, clazz, kind);
        System.out.println(stu);
        List<Student> studentList = new ArrayList<>();
        for (int i = 0; i < 8; ++i) {
            sex = i % 2 == 0 ? "男" : "女";
            studentList.add(new Student(name + i, stuId, age + i, sex, major, clazz, kind));
        }
        request.setAttribute("list", studentList);
        request.setAttribute("stu", stu);
        request.getRequestDispatcher("ch2/info.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
